package State 
{
	import Box2DAS.Dynamics.ContactEvent;
	import com.citrusengine.core.CitrusEngine;
	import com.citrusengine.core.CitrusObject;
	import com.citrusengine.core.State;
	import com.citrusengine.objects.CitrusSprite;
	import com.citrusengine.objects.platformer.Baddy;
	import com.citrusengine.objects.platformer.Hero;
	import com.citrusengine.objects.platformer.MovingPlatform;
	import com.citrusengine.objects.platformer.Platform;
	import com.citrusengine.objects.platformer.Sensor;
	import com.citrusengine.physics.Box2D;
	import com.citrusengine.math.MathVector;
	import com.citrusengine.physics.simple.CitrusSolver;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	/**
	 * ...
	 * @author Haxx
	 */
	
	public class UpgradeState extends Sprite
	{
		//--------------------------------
		// Background Sprite
		//--------------------------------
		[Embed(source="../../bin/GG_ui.swf", symbol="GameOverUI")]
		public var cGameOver:Class;
		private var sprGameOver:Sprite;
		
		//--------------------------------
		// Symbol Instance Names
		//--------------------------------
		private const BTN_BUYLIFE:String = "buyLifeBtn";
		private const BTN_BUYSPEED:String = "buyElevatorBtn";
		private const BTN_RESTART:String = "restartBtn";
		
		private const FLD_LEVEL:String = "levelField";
		private const FLD_KILLS:String = "killField";
		private const FLD_MONEY:String = "moneyField";
		private const FLD_ELEVATOR_PRICE:String = "elevatorPriceField";
		private const FLD_LIFE_PRICE:String = "lifePriceField";
		private const FLD_ELEVATOR_LEVEL:String = "elevatorLevelField";
		private const FLD_LIFE_LEVEL:String = "lifeLevelField";
		
		//--------------------------------
		// Level Results
		//--------------------------------
		private var upgradeElevatorCost:int;
		private var upgradeLifeCost:int;
		private var floor:int;
		private var kills:int;
		
		
		public function UpgradeState(floorCoins:int, killCoins:int) 
		{
			this.floor = floorCoins;
			this.kills = killCoins;
			PlayerUpgrades.playerCoins += (floorCoins + killCoins);
			trace(floorCoins + " , " + killCoins);
			initialize();
			
			this.alpha = 0;
			addEventListener(Event.ENTER_FRAME, fadeIn);
			
		}
		
		private function fadeIn(e:Event):void
		{
			this.alpha += 0.05;
			if(this.alpha >= 1)
				removeEventListener(Event.ENTER_FRAME, fadeIn);
		}
		
		public function initialize():void 
		{	
			trace("COINS: " + PlayerUpgrades.playerCoins);
			updateUpgradeCosts();
			
			sprGameOver = new cGameOver();
			addChild(sprGameOver);
			
			sprGameOver[BTN_BUYLIFE].addEventListener(MouseEvent.CLICK, upgradeLife);
			sprGameOver[BTN_BUYSPEED].addEventListener(MouseEvent.CLICK, upgradeElevator);
			sprGameOver[BTN_RESTART].addEventListener(MouseEvent.CLICK, continueGame);
			
			updateFields();
		}
		
		public function upgradeLife(e:MouseEvent):void
		{
			
			if (PlayerUpgrades.playerCoins > upgradeLifeCost)
			{
				PlayerUpgrades.lvlMaxLife += 1;
				PlayerUpgrades.playerCoins -= upgradeLifeCost;
			}
			else
				trace("NOT ENOUGH MONEY");
				
			updateUpgradeCosts();
			updateFields();
		}
		public function upgradeElevator(e:MouseEvent):void
		{
			trace(PlayerUpgrades.playerCoins + " , " + upgradeElevatorCost);
			if (PlayerUpgrades.playerCoins > upgradeElevatorCost)
			{
				PlayerUpgrades.lvlElevatorSpeed += 1;
				PlayerUpgrades.playerCoins -= upgradeElevatorCost;
			}
			else
				trace("NOT ENOUGH MONEY");
			
			updateUpgradeCosts();
			updateFields();
		}
		
		public function continueGame(e:MouseEvent):void
		{
			sprGameOver[BTN_BUYLIFE].removeEventListener(MouseEvent.CLICK, upgradeLife);
			sprGameOver[BTN_BUYSPEED].removeEventListener(MouseEvent.CLICK, upgradeElevator);
			sprGameOver[BTN_RESTART].removeEventListener(MouseEvent.CLICK, continueGame);
			
			this.stage.focus = this.stage;
			CitrusEngine.getInstance().state = new State.GameState(1);
			
		}
		
		public function updateUpgradeCosts():void
		{
			upgradeElevatorCost = GameBalance.SPEED_COST * PlayerUpgrades.lvlElevatorSpeed;
			upgradeLifeCost = GameBalance.LIFE_COST * PlayerUpgrades.lvlMaxLife;
			
			//save player data
			PlayerRecords.saveData();
		}
		
		public function updateFields():void
		{
			
			sprGameOver[FLD_ELEVATOR_PRICE].text = "" + upgradeElevatorCost;
			sprGameOver[FLD_LIFE_PRICE].text = "" + upgradeLifeCost;
			sprGameOver[FLD_ELEVATOR_LEVEL].text = "" + PlayerUpgrades.lvlElevatorSpeed;
			sprGameOver[FLD_LIFE_LEVEL].text = "" + PlayerUpgrades.lvlMaxLife;
			
			sprGameOver[FLD_LEVEL].text = "" + floor;
			sprGameOver[FLD_KILLS].text = "" + kills;
			sprGameOver[FLD_MONEY].text = "" + PlayerUpgrades.playerCoins;
		}
		
		
		
	}

}